package org.zoomdev.zoom.common.utils;

import java.lang.reflect.Type;

public final class Types {

    /**
     * obj是否是type的实现类 , 与instanceOf的语义不同， obj为null有效
     *
     * @param obj
     * @param type
     * @return
     */
    public static boolean matches(Object obj, Type type) {
        if (obj == null) {
            return !isPrime(type);
        }

        return obj instanceof Type;
    }

    public static boolean isPrime(Type type) {
        return (type instanceof Class) && ((Class) type).isPrimitive();
    }

    /**
     * args是否全部都满足types的需求
     *
     * @param types
     * @param args
     * @return
     */
    public static boolean matches(Type[] types, Object[] args) {
        assert (types != null);
        assert (args != null);
        assert (types.length == args.length);
        for (int i = 0; i < types.length; ++i) {
            Type type = types[i];
            Object arg = args[i];
            if (!Types.matches(arg, type)) {
                return false;
            }
        }
        return true;
    }


}
